#pragma once
#include <string>
#include <memory> // std::auto_ptr
#include <cstdlib> // std::exit
#include <iostream>
#include <odb/database.hxx>
#include <odb/mysql/database.hxx>
#include "logger.hpp"
#include "friend_apply.hxx"
#include "friend_apply-odb.hxx"
#include "mysql.hpp"

namespace zrb
{
    //使用ODB框架对会话成员表的操作
    class FriendApplyTable
    {
    public:
        using ptr = std::shared_ptr<FriendApplyTable>;
        FriendApplyTable(const std::shared_ptr<odb::core::database>& db)
            :_db(db)
        {}

        //新增好友申请
        bool insert(Friend_Apply& ev) 
        {
            try 
            {
                //3.获取事务对象开启事务
                odb::transaction trans(_db->begin());
                _db->persist(ev);
                //4.提交事务
                trans.commit();
            }
            catch (std::exception &e) 
            {
                LOG_ERROR("新增好友申请事件出错：{}-{} : {}",ev.user_id(),ev.peer_id(),e.what());
                return false;
            }
            return true;
        }

        //判断是否存在好友申请
        bool exists(const std::string &uid, const std::string &pid) {
            bool flag = false;
            try {
                typedef odb::query<Friend_Apply> query;
                typedef odb::result<Friend_Apply> result;
                odb::transaction trans(_db->begin());
                result r(_db->query<Friend_Apply>(query::user_id == uid && query::peer_id == pid));
                LOG_DEBUG("{} - {} 好友事件数量：{}", uid, pid, r.size());
                flag = !r.empty();
                trans.commit();
            }
            catch (std::exception &e) 
            {
                LOG_ERROR("获取好友申请事件失败:{}-{}-{}！", uid, pid, e.what());
            }
            return flag;
        }

        //删除好友申请
        bool remove(const std::string& uid,const std::string& pid)
        {
            try 
            {
                //3.获取事务对象开启事务
                odb::transaction trans(_db->begin());
                typedef odb::query<Friend_Apply> query;
                typedef odb::result<Friend_Apply> result;
                _db->erase_query<Friend_Apply>(query::user_id == uid && query::peer_id == pid); //删除条件
                //4.提交事务
                trans.commit();
            }
            catch (std::exception &e) 
            {
                LOG_ERROR("删除好友申请数据出错：{}-{} {}",uid,pid,e.what());
                return false;
            }
            return true;
        }

        //获取当前指定用户的所有好友申请者ID
        std::vector<std::string> apply_users(const std::string& uid) //被申请者ID
        {
            std::vector<std::string> res;
            try {
                //获取事务对象开启事务
                odb::transaction trans(_db->begin());
                typedef odb::query<Friend_Apply> query;
                typedef odb::result<Friend_Apply> result;

                //查询
                result r(_db->query<Friend_Apply>(query::peer_id == uid));

                //遍历
                for (auto it = r.begin(); it != r.end(); ++it) 
                {
                    res.push_back(it->user_id());
                }
                //5. 提交事务
                trans.commit();
            }
            catch (std::exception &e) 
            {
                LOG_ERROR("通过用户好友申请者ID出错：{}",e.what());
            }
            return res;
        }

    private:
        std::shared_ptr<odb::core::database> _db; 
    };
}